The registrar has student profiles, course details, and enrolments in separate tables. Build a report that puts the right student name beside the right course.
Your Three Requests
- Find Emma Li's active courses.
- List active enrolments with student names and course titles.
- Deliver the active PY101 roster for DSAI students.
Prepare PostgreSQL
- In pgAdmin, open the supplied
lab6_7_setup.sqlfrom the TA folderDSAI2202/lab6_7/and run the complete file once. - Confirm its final counts: 22 students, 8 courses, 37 enrolments.
- Run the connection setting on the right. Repeat it whenever you open a new database connection.
- Inspect the three tables before joining them.
Lab 7 uses the same setup, including assignments, submissions, and lms_roster. Today, use the three tables shown here. All people and records are fictional.
✍️ Trace Before You Join
Find enrolment E01. Which student ID and course code does it contain? Use the Data tab to recover the student's name and the course title.
SET search_path TO lab6_7;SET search_path TO lab6_7;
SELECT student_id, student_name, programme
FROM students
ORDER BY student_id;SET search_path TO lab6_7;
SELECT course_code, course_title, credits
FROM courses
ORDER BY course_code;SET search_path TO lab6_7;
SELECT enrolment_id, student_id, course_code,
enrolment_status, score
FROM enrolments
ORDER BY enrolment_id;Small Previews of the Three Tables
students: 22 rows. student_id is the primary key.
| student_id | student_name | programme |
|---|---|---|
| S101 | Alice Wong | DSAI |
| S102 | Ben Lau | CS |
| S105 | Emma Li | CS |
courses: 8 rows. course_code is the primary key.
| course_code | course_title | credits |
|---|---|---|
| SQL101 | SQL Fundamentals | 3 |
| PY101 | Python Programming | 3 |
| WEB101 | Web Databases | 3 |
enrolments: 37 rows, including 30 active and 7 withdrawn. enrolment_id is the primary key.
| enrolment_id | student_id | course_code | enrolment_status |
|---|---|---|---|
| E01 | S101 | SQL101 | active |
| E04 | S104 | SQL101 | withdrawn |
| E06 | S105 | PY101 | active |
Student link
enrolments.student_id → students.student_idA foreign key points to one student.
Course link
enrolments.course_code → courses.course_codeA foreign key points to one course.
Trace E01
S101 + SQL101Alice Wong + SQL Fundamentals.
The repeated IDs connect records; the names and titles stay in their own tables. One student may appear in several enrolments.